﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FrameWork.Lib;

namespace AnimeWatchMax.XSD
{
    public static class AnimeSukiHandler
    {
        public static AnimeSukiDataSet GetAnimeSukiDataSetFromXml(string pXML)
        {
            var tNewDataSet = new AnimeSukiDataSet();

            using (var sr = new System.IO.StringReader(pXML))
            {
                tNewDataSet.ReadXml(sr, System.Data.XmlReadMode.IgnoreSchema);
            }

            return tNewDataSet;

        }

        public static void ProcessAnimeSukiXMLToData(EventManager pEventManager, string pXml, DataObjects.Data pData)
        {
            var test = AnimeWatchMax.XSD.AnimeSukiHandler.GetAnimeSukiDataSetFromXml(pXml);

            foreach (AnimeWatchMax.XSD.AnimeSukiDataSet.releaseRow tReleaseRow in test.release.Rows)
            {
                var tEpisodeRows = tReleaseRow.GetepisodeRows();
                var tGroupRows = tReleaseRow.GetgroupRows();
                var tTorrentRows = tReleaseRow.GettorrentRows();
                var tVersionRows = tReleaseRow.GetversionRows();
                var tFileRows = tReleaseRow.GetfileRows();

                var titleid = tReleaseRow.titleid;
                var release_Id = tReleaseRow.release_Id;
                var filesize = tReleaseRow.filesize;
                var filename = tReleaseRow.filename;
                var batch = tReleaseRow.batch;
                var animesuki_Id = tReleaseRow.animesuki_Id;
                var tTitleID = tReleaseRow.id.IntValueOrNull();
                if(tTitleID == null) continue;

                var tTitle = pData.TitleList.Where(t => t.ID == tTitleID.Value).FirstOrDefault();

                if (tTitle == null)
                {
                    tTitle = new DataObjects.Title(tTitleID.Value)
                    {
                        Description = tReleaseRow.description.StringValueOrEmpty(),
                        Name = tReleaseRow.title.StringValueOrEmpty(),
                        TitleCreatedDate = tReleaseRow.added.UNIXDateValueOrNull(),
                        Status = tReleaseRow.status.ToTitleStatus(),
                        State = TitleStateType.New,
                        ReleasedDate = tReleaseRow.released.UNIXDateValueOrNull()
                    };

                    pData.TitleList.Add(tTitle);
                    pEventManager.AddResult(Result.NewWarning("New Title Added {0}", tTitle.Name));
                    pEventManager.AddTitleItem(tTitle);
                    
                }
                else
                {
                    tTitle.Updated = DateTime.Now;
                    tTitle.Description = tReleaseRow.description.StringValueOrEmpty();
                    tTitle.Name = tReleaseRow.title.StringValueOrEmpty();
                    tTitle.TitleCreatedDate = tReleaseRow.added.UNIXDateValueOrNull();
                    tTitle.Status = tReleaseRow.status.ToTitleStatus();
                    tTitle.ReleasedDate = tReleaseRow.released.UNIXDateValueOrNull();
                }

                var tEpisodeRow = tEpisodeRows.FirstOrDefault();

                if (tEpisodeRow != null)
                {
                    var tEpisode = tTitle.EpisodeList.Where(t => t.EpisodeText == tEpisodeRow.episode_Text).FirstOrDefault();

                    if (tEpisode != null)
                    {
                        tEpisode.Updated = DateTime.Now;
                    }
                    else
                    {
                        tEpisode = new DataObjects.Episode(tEpisodeRow.order, tEpisodeRow.episode_Text);
                        tTitle.EpisodeList.Add(tEpisode);
                        pEventManager.AddResult(Result.NewWarning("New Episode {0} Added For Title {1}", tEpisode.EpisodeText, tTitle.Name));
                    }

                    var tFile = tEpisode.FileList.Where(t => t.Name == filename).FirstOrDefault();

                    if (tFile != null)
                    {
                        tFile.Updated = DateTime.Now;
                    }
                    else
                    {
                        tFile = new DataObjects.File(filename);
                        tEpisode.FileList.Add(tFile);
                    }

                    foreach (var tGroupRow in tGroupRows)
                    {
                        if (!tFile.GroupIDList.Contains(tGroupRow.id))
                        {
                            tFile.GroupIDList.Add(tGroupRow.id);
                        }

                        if (!pData.GroupList.Exists(t => t.ID == tGroupRow.id))
                        {
                            pData.GroupList.Add(new DataObjects.Group(tGroupRow.id) { ShortName = tGroupRow.abbreviation, LongName = tGroupRow.name });
                        }
                    }

                    var tFileNameList = tFile.FileNameList.Select(t => t.FileName).ToList();
                    foreach (var tFileRow in tFileRows)
                    {
                        if (!tFileNameList.Contains(tFileRow.name))
                        {
                            tFile.FileNameList.Add(new DataObjects.FileNameSize() { FileName = tFileRow.name, Size = tFileRow.size.IntValueOrZero() });
                        }
                    }


                    foreach (var tTorrentRow in tTorrentRows)
                    {
                        var tTorrent = tFile.TorrentList.Where(t => t.ID == tTorrentRow.id).FirstOrDefault();

                        if (tTorrent != null)
                        {
                            tTorrent.Updated = DateTime.Now;
                        }
                        else
                        {
                            tTorrent = new DataObjects.Torrent(tTorrentRow.id);
                            tFile.TorrentList.Add(tTorrent);
                        }

                        tTorrent.UL = tTorrentRow.ul.IntValueOrZero();
                        tTorrent.DL = tTorrentRow.dl.IntValueOrZero();
                        tTorrent.Downloaded = tTorrentRow.downloaded.IntValueOrZero();
                        tTorrent.Direct = tTorrentRow.direct;
                        var tSite = tTorrentRow.GetsiteRows().FirstOrDefault();
                        if (tSite != null)
                        {
                            tTorrent.Site = tSite.site_Text;
                            tTorrent.SiteHotLink = tSite.hotlink.BoolValue();
                        }
                        else
                        {
                            tTorrent.Site = "";
                            tTorrent.SiteHotLink = false;
                        }
                        tTorrent.TrackerProblem = tTorrentRow.trackerproblem.BoolValue();
                        tTorrent.Status = tTorrentRow.status.StringValueOrEmpty().ToTorrentStatus();
                    }

                }


            }
        }
    }
}
